package blackstar.plugin.ui;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import blackstar.plugin.ui.vo.OverViewInfo;

public class OverviewServlet extends HttpServlet
{
	private final static Log LOGGER = LogFactory.getLog(OverviewServlet.class);

	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		String uri = request.getRequestURI();
		if (!"/".equals(request.getContextPath()))
		{
			uri = uri.substring(request.getContextPath().length());
		}
		LOGGER.info("request:" + uri);
		String[] pathArray = uri.substring(1).split("/");

		if (pathArray.length == 2)
		{
			String jvmid = pathArray[1];
			printJVMOverview(request, response, jvmid);
		} else
		{
			response.sendError(404);
		}
	}

	private void printJVMOverview(HttpServletRequest request,
			HttpServletResponse response, String jvmid) throws IOException
	{
		response.setContentType("application/json;charset=utf-8");
		OverViewInfo overview = JMXProxyUtils.getJVMOverViewInfo(jvmid);
		if (overview == null)
		{
			LOGGER.info("error jvmid[" + jvmid + "]");
			response.sendError(404);
		} else
		{
			PrintWriter writer = response.getWriter();

			writer.print("{");
			writer.print("jmxServiceURL : '");
			writer.print(overview.getJmxServiceURL());
			writer.print("', runtimeStartTime :'");
			writer.print(overview.getRuntimeStartTime());
			writer.print("', runtimeUpTime :'");
			writer.print(overview.getRuntimeUpTime());
			writer.print("', runtimeName :'");
			writer.print(overview.getRuntimeName());
			writer.print("', runtimeVmvendor :'");
			writer.print(overview.getRuntimeVmvendor());
			writer.print("', runtimeVmname :'");
			writer.print(overview.getRuntimeVmname());
			writer.print("', runtimeVmversion :'");
			writer.print(overview.getRuntimeVmversion());
			writer.print("', runtimeStartTime :'");
			writer.print(overview.getRuntimeStartTime());
			writer.print("', runtimeVmArgs :'");
			writer.print(StringEscapeUtils.escapeJavaScript(overview
					.getRuntimeVmArgs()));
			writer.print("', runtimeClassPath :'");
			writer.print(StringEscapeUtils.escapeJavaScript(overview
					.getRuntimeClassPath()));
			writer.print("', runtimeLibraryPath :'");
			writer.print(StringEscapeUtils.escapeJavaScript(overview
					.getRuntimeLibraryPath()));
			writer.print("', runtimeBootClassPath :'");
			writer.print(StringEscapeUtils.escapeJavaScript(overview
					.getRuntimeBootClassPath()));
			writer.print("', compilationName :'");
			writer.print(overview.getCompilationName());
			writer.print("', compilationTime :'");
			writer.print(overview.getCompilationTime());
			writer.print("', memoryHeap :'");
			writer.print(overview.getMemoryHeap());
			writer.print("', memoryNonheap :'");
			writer.print(overview.getMemoryNonheap());
			writer.print("', memoryGcs :[");
			int idx = 0;
			for (String gc : overview.getMemoryGcs())
			{
				if (idx > 0)
				{
					writer.print(",");
				}
				writer.print("'");
				writer.print(gc);
				writer.print("'");

				idx++;
			}
			writer.print("]}");

			writer.flush();
		}
	}
}
